using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Eve.Data
{
    public class Faction : EveObject
    {
        private string factionName;
        private string description;
        private List<int> raceIds = new List<int>();
        private int solarSystemId;
        private int corporationId;
        private double sizeFactor;
        private int stationCount;
        private int stationSystemCount;

        private SolarSystem solarSystem = null;
        private Corporation corporation = null;
        private List<Race> races = null;

        private List<ContrabandType> contraband;

        public string Name
        {
            get { return factionName; }
        }
        public string Description
        {
            get { return description; } 
        }
        public List<Race> Races
        {
            get
            {
                if (races == null)
                {
                    races = new List<Race>();
                    foreach (int id in raceIds)
                        races.Add(new Race(db, id));
                }
                return races;
            }
        }
        public SolarSystem SolarSystem
        {
            get
            {
                if (solarSystem == null)
                    solarSystem = new SolarSystem(db, solarSystemId);
                return solarSystem;
            }
        }
        public Corporation Corporation
        {
            get
            {
                if (corporation == null)
                    corporation = new Corporation(db, corporationId);
                return corporation;
            }
        }
        public double SizeFactor
        {
            get { return sizeFactor; }
        }
        public int StationCount
        {
            get { return stationCount; }
        }
        public int StationSystemsCount
        {
            get { return stationSystemCount; }
        }
        public List<ContrabandType> Contraband
        {
            get
            {
                if (contraband == null)
                {
                    contraband = new List<ContrabandType>();
                    string query = "SELECT * FROM invContrabandTypes WHERE factionID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        contraband.Add(new ContrabandType(row, this));
                }
                return contraband;
            }
        }

        internal Faction(DbDataRow dr)
            : base(dr.Database, EveObjectType.Faction, dr["factionID"])
        {
            Fill(dr);
        }
        internal Faction(Database db, int id)
            : base(db, EveObjectType.Faction, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM chrFactions WHERE factionID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            factionName = dr["factionName"];
            description = dr["description"];
            int rIds = dr["raceIDs"];
            for (int i = 1; i != 0; i <<= 1)
                if ((rIds & i) != 0)
                    raceIds.Add(i);
            solarSystemId = dr["solarSystemID"];
            corporationId = dr["corporationID"];
            sizeFactor = dr["sizeFactor"];
            stationCount = dr["stationCount"];
            stationSystemCount = dr["stationSystemCount"];
        }
    }
}
